<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class VipSettingsModel extends Model
{
	const ACTIVE_STATUS = 1;

	protected $connection = 'db_main';

	protected $table = 'wp_sdk_member_vip_settings';


	public $timestamps = false;


	/*获取某项数据*/
	public static function getItemValue($uid, $key, $default = 0)
	{
		$conf = self::where(["uid" => $uid, "item_key" => $key, "status" => self::ACTIVE_STATUS]) -> first();
		if($conf && $conf->item_value) {
			return $conf->item_value;
		}
		return $default;
	}

	/*更新某项数据*/
	public static function updateItemValue($uid, $key, $value)
	{
		$row = self::where(["uid" => $uid, "item_key" => $key, "status" => self::ACTIVE_STATUS]) -> first();
		if($row->id) {
			$row->item_value += $value;
			$row->modify_time = time();
			return $row->save();
		} else {
			return self::create([
				"uid" => $uid,
				"item_key" => $key, 
				"item_value" => $value,
				"status" => self::ACTIVE_STATUS,
				"create_time" => time(),
				"modify_time" => 0
			]);
		}
	}

	/*获取购买的功能信息*/
	public static function getProductItemKey($userId, $appId, $productId)
	{
		return 'product_' . intval($appId) . '_' . intval($productId) . '_endtime';
	}

  	
}

